package remote.utils;

import java.io.StringReader;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;

public class XmlParser
{
	public static String[] parseXML(String xml)
	{
		int i = 0;
		SAXBuilder saxBuilder = new SAXBuilder();
		StringReader reader = new StringReader(xml);
		Document xmlDocument;
		
		try
		{
			xmlDocument = saxBuilder.build(reader);
			Element root = xmlDocument.getRootElement();
			List<?> content = root.getContent();
			Iterator<?> iterator = content.iterator();
			String[] values = new String[content.size()];
			
			while(iterator.hasNext())
			{
				Object object = iterator.next();
				
				if(object instanceof Element)
					values[i] = ((Element)object).getValue();
				else if(object instanceof Text)
					values[i] = ((Text)object).getText();
				
				i++;
			}
			
			return values;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
}
